SOCK_STREAM and the ai_protocol member should be set to IPPROTO_TCP when
ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) is called. If the caller handles only IPv4 and not IPv6,
then the ai_family member of the _h_i_n_t_s structure should be set to AF_INET
when ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) is called. If the third argument to ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))) is
a NULL pointer, this is the same as if the caller had filled in an
addrinfo structure initialized to zero with ai_family set to AF_UNSPEC.
The ai_flags field to which _h_i_n_t_s parameter points must have the value
zero or be the bitwise OR of one or more of the values AI_PASSIVE,
AI_CANONNAME, AI_NUMERICHOST, AI_NUMERICSERV, AI_V4MAPPED, AI_ALL, and
AI_ADDRCONFIG.
If the AI_PASSIVE bit is set in the ai_flags member of the _h_i_n_t_s
structure, then the caller plans to use the returned socket address
structure in a call to bind(). In this case, if the _n_o_d_e_n_a_m_e argument is
a NULL pointer, then the IP address portion of the socket address
structure will be set to INADDR_ANY for an IPv4 address or
IN6ADDR_ANY_INIT for an IPv6 address.
If the AI_PASSIVE bit is not set in the ai_flags member of the hints
structure, then the returned socket address structure will be ready for a
call to ccccoooonnnnnnnneeeecccctttt(((()))) (for a connection-oriented protocol) or either
ccccoooonnnnnnnneeeecccctttt(((()))),,,, sssseeeennnnddddttttoooo(((()))),,,, oooorrrr sssseeeennnnddddmmmmssssgggg(((()))) (for a connectionless protocol). In
this case, if the _n_o_d_e_n_a_m_e argument is a NULL pointer, then the IP
address portion of the socket address structure will be set to the
loopback address.
If the AI_CANONNAME bit is set in the ai_flags member of the _h_i_n_t_s
structure, then upon successful return the ai_canonname member of the
first addrinfo structure in the linked list will point to a
null-terminated string containing the canonical name of the specified
_n_o_d_e_n_a_m_e. If the canonical name is not available, then ai_canonname will
refer to the nodename argument or a string with the same contents.
If the AI_NUMERICHOST bit is set in the ai_flags member of the _h_i_n_t_s
structure, then a non-NULL _n_o_d_e_n_a_m_e string must be a numeric host address
string. Otherwise an error of EAI_NONAME is returned. This flag
prevents any type of name resolution service (e.g., the DNS) from being
called.
If the flag AI_NUMERICSERV is specified then a non-null servname string
must be a numeric port string. Otherwise an error EAI_NONAME is
returned. This flag prevents any type of name resolution service (for
example, NIS+) from being invoked.
If the AI_V4MAPPED flag is specified along with an ai_family of AF_INET6,
then the caller will accept IPv4-mapped IPv6 addresses. That is, if no
AAAA or A6 records are found then a query is made for A records and any
found are returned as IPv4-mapped IPv6 addresses (ai_addrlen will be 16).
The AI_V4MAPPED flag is ignored unless ai_family equals AF_INET6.
To aid applications in printing error messages based on the EAI_xxx codes
returned by ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))),,,, ggggaaaaiiii____ssssttttrrrreeeerrrrrrrroooorrrr(((()))) is defined. The argument is
one of the EAI_xxx values defined earlier and the return value points to
a string describing the error. If the argument is not one of the EAI_xxx
values, the function still returns a pointer to a string whose contents
indicate an unknown error.
EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
The following code tries to connect to "www.company.example" service
"ftp" via stream socket. It loops through all the addresses available,
regardless of the address family. If the destination resolves to IPv4
address, it will use AF_INET socket. Similarly, if it resolves to IPv6,
AF_INET6 socket is used. Observe that there is not hardcoded reference
The ggggeeeettttaaaaddddddddrrrriiiinnnnffffoooo(((()))),,,, ffffrrrreeeeeeeeaaaaddddddddrrrriiiinnnnffffoooo(((()))),,,, and ggggaaaaiiii____ssssttttrrrreeeerrrrrrrroooorrrr(((()))) functions are
defined in IEEE POSIX 1003.1g draft specification, draft-ietf-ipngwg-
rfc2553bis-03.txt, and documented in "Basic Socket Interface Extensions